package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.entity.WebMenuEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebRolesEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebRolesmenuRlatEntity;
import com.baitengsoft.patentapimonitor.api.model.WebRolesViewModel;
import com.baitengsoft.patentapimonitor.api.model.WebRolesmenuRlatViewModel;
import com.baitengsoft.patentapimonitor.api.service.WebMenuService;
import com.baitengsoft.patentapimonitor.api.service.WebRolesService;
import com.baitengsoft.patentapimonitor.api.service.WebRolesmenuRlatService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Admin on 2017/12/18.
 */

@Service
public class WebRolesServiceImpl extends BaseServiceImpl<WebRolesEntity> implements WebRolesService{
    @Autowired
    private WebRolesmenuRlatService webrolesmenurlatservice;
    @Autowired
    private WebMenuService webMenuService;
    @Override
    public PageResult<WebRolesmenuRlatViewModel> getRoles(WebRolesViewModel model) {

            PageHelper.startPage(model.getPage(),model.getPageSize());
            Mapper<WebRolesEntity> mapper = this.getMapper();
            Example example=new Example(WebRolesEntity.class);
            example.setOrderByClause("createdate DESC");
            Example.Criteria createCriteria=example.createCriteria();
            createCriteria.andEqualTo("status",1);
            if(StringUtils.isNotBlank(model.getRolesName())){
                createCriteria.andLike("name","%"+model.getRolesName().trim()+"%");
            }
            //example.createCriteria().andLike("name","%"+model.getRolesName()+"%").andEqualTo("status",1);
            List<WebRolesEntity> entities=mapper.selectByExample(example);
            if(entities==null||entities.size()==0) return null;//暂无数据
            PageInfo<WebRolesEntity> pageEntities=new PageInfo<>(entities);
            List<WebRolesmenuRlatViewModel> models=new ArrayList<>();
            for(WebRolesEntity pageEntity:pageEntities.getList()){
                WebRolesmenuRlatViewModel viewModel=new WebRolesmenuRlatViewModel();
                List<WebRolesmenuRlatEntity> webrolesmenurlatentities=webrolesmenurlatservice.searchByRolesID(pageEntity.getId());
                //if(webrolesmenurlatentities==null)return null;
                if(webrolesmenurlatentities!=null&&webrolesmenurlatentities.size()>0){
                StringBuffer sb=new StringBuffer();
                int num=0;
                try{
                    for(int i=0;i<webrolesmenurlatentities.size();i++){
                        WebMenuEntity webmenuentity=webMenuService.searchById(webrolesmenurlatentities.get(i).getMenuid());
                        if(webmenuentity.getParentid()>0){
                            continue;
                        }
                        num++;
                        if(num<7){
                            sb.append(webmenuentity.getName()).append("、");
                        }
                    }
                }
                catch (Exception ex){
                    ex.printStackTrace();
                    System.out.println(ex);
                }
                finally {
                    if(sb.length()>0){
                        String menuNameStr=sb.substring(0,sb.length()-1);
                        viewModel.setMenuName(menuNameStr);
                    }

                }


                }else {
                    viewModel.setMenuName("");
                }
                viewModel.setId(pageEntity.getId());
                viewModel.setRolesName(pageEntity.getName());
                models.add(viewModel);
            }
            return new PageResult<WebRolesmenuRlatViewModel>(models,pageEntities.getPageNum(),pageEntities.getPageSize(),(int)pageEntities.getTotal());
    }

    @Override
    @Transactional
    public Integer saveRoles(String roleName,int userId) {
        WebRolesEntity entity=new WebRolesEntity();
        entity.setName(roleName);
        entity.setStatus(1);
        entity.setSort(1);
        entity.setCreatedate(new Date());
        entity.setUpdatedate(new Date());
        entity.setCreateoperid(userId);
        entity.setUpdateoperid(userId);
        Integer successId= saveByNotNull(entity);
        if(successId==0){//保存失败
            return 0;
        }
        return entity.getId();//保存成功
    }

    @Override
    public Integer updateById(Integer id,String rolesName,int userId) {
        WebRolesEntity entity=new WebRolesEntity();
        entity.setId(id);
        entity.setName(rolesName);
        entity.setUpdatedate(new Date());
        entity.setUpdateoperid(userId);
        Integer successId=updateByNotNull(entity);
        return successId;
    }

    @Override
    @Transactional
    public Integer delRolesById(String ids, int userId) {

        if(StringUtils.isNotBlank(ids)){
            String[] idArray=ids.split(",");
            WebRolesEntity entity=new WebRolesEntity();
            entity.setStatus(-1);
            entity.setUpdateoperid(userId);
            entity.setUpdatedate(new Date());
            for(String id:idArray){
                entity.setId(Integer.parseInt(id));
                Integer successId=updateByNotNull(entity);
                if(successId==0) return 0;//更新失败
            }
            return 1;
        }
        return 0;
    }

    @Override
    public WebRolesEntity searchById(String id) {

        WebRolesEntity entity=queryById(Integer.parseInt(id));
        if(entity==null) return null;
        return entity;
    }

    @Override
    public List<WebRolesEntity> searchModels() {

        WebRolesEntity entity=new WebRolesEntity();
        entity.setStatus(1);
        List<WebRolesEntity> entities=queryListByWhere(entity);
        if(entities!=null&&entities.size()>0) return entities;
        return null;
    }

    @Override
    public Boolean checkRolesName(String rolesName) {

        WebRolesEntity entity=new WebRolesEntity();
        entity.setStatus(1);
        entity.setName(rolesName);
        List<WebRolesEntity> entities=queryListByWhere(entity);
        if(entities!=null&&entities.size()>0) return false;
        return true;
    }
}
